﻿using System.Collections.Generic;
using Abp.Configuration;
using Microsoft.Extensions.Configuration;

namespace Sunlit.Business.Configuration
{
    public class AppSettingProvider : SettingProvider
    {
        private readonly IConfigurationRoot _appConfiguration;

        public AppSettingProvider(IAppConfigurationAccessor configurationAccessor)
        {
            _appConfiguration = configurationAccessor.Configuration;
        }
        public override IEnumerable<SettingDefinition> GetSettingDefinitions(SettingDefinitionProviderContext context)
        {
            return GetSiteConfigSettings();
        }
        private IEnumerable<SettingDefinition> GetSiteConfigSettings()
        {
            return new[]
            {
                new SettingDefinition(AppSettingNames.SiteConfig.EnableSms,GetFromSettingsBySettingName(AppSettingNames.SiteConfig.EnableSms,"false"),scopes: SettingScopes.Application, isVisibleToClients: true),
                new SettingDefinition(AppSettingNames.SiteConfig.Company,GetFromSettingsBySettingName(AppSettingNames.SiteConfig.Company,""),scopes: SettingScopes.Application, isVisibleToClients: true),
                new SettingDefinition(AppSettingNames.SiteConfig.HotLine,GetFromSettingsBySettingName(AppSettingNames.SiteConfig.HotLine,""),scopes: SettingScopes.Application, isVisibleToClients: true),
                new SettingDefinition(AppSettingNames.SiteConfig.KeyWords,GetFromSettingsBySettingName(AppSettingNames.SiteConfig.KeyWords,""),scopes: SettingScopes.Application, isVisibleToClients: true),
                new SettingDefinition(AppSettingNames.SiteConfig.Description,GetFromSettingsBySettingName(AppSettingNames.SiteConfig.Description,""),scopes: SettingScopes.Application, isVisibleToClients: true)
            };
        }
        /// <summary>
        /// 从配置文件的App节点获取配置
        /// </summary>
        /// <param name="name"></param>
        /// <param name="defaultValue"></param>
        /// <returns></returns>
        private string GetFromAppSettings(string name, string defaultValue = null)
        {
            return GetFromSettings("App:" + name, defaultValue);
        }
        /// <summary>
        /// 从配置文件获取配置
        /// </summary>
        /// <param name="name"></param>
        /// <param name="defaultValue"></param>
        /// <returns></returns>
        private string GetFromSettings(string name, string defaultValue = null)
        {
            return _appConfiguration[name] ?? defaultValue;
        }
        /// <summary>
        /// 从配置文件获取配置
        /// </summary>
        /// <param name="name"></param>
        /// <param name="defaultValue"></param>
        /// <returns></returns>
        private string GetFromSettingsBySettingName(string name, string defaultValue = null)
        {
            name = name.Replace(".", ":");
            return _appConfiguration[name] ?? defaultValue;
        }
    }
}
